﻿using Iworld.Author.Manager;
using Iworld.Author.Service.Client.Export;
using Iworld.Author.Service.Client.Import;
using Iworld.basis.Service;
using Iworld.Game.Manager;
using Iworld.Main.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace Iworld.Author.Service.Client
{
    /// <summary>
    /// 前台(游戏大厅)用户服务
    /// </summary>
    public class AuthorService : IAuthorService
    {

        #region 查询数据

        public NormalResult<HeartbeatExport> KeepHeartbeat(KeepHeartbeatImport import)
        {
            try
            {
                NormalResult<HeartbeatExport> ret = null;
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    using (ModelToDbContext db = new ModelToDbContext())
                    {
                        AuthorLoginInfoPond.KeepHeartbeat(import.Token);
                        Model.Author self = AuthorLoginInfoPond.GetUserInfo(db, import.Token);
                         SettingOfGame sg = new SettingOfGame(db);
                        SettingOfGameExport soge = new SettingOfGameExport(sg);
                        ret = new NormalResult<HeartbeatExport>(
                            new HeartbeatExport() { SelfInfo = new AuthorExport(self), SettingOfGame = soge });
                        ts.Complete();
                    }
                }
                return ret;
            }
            catch (Exception ex)
            {
                 string msg = ex.Message;
                return new NormalResult<HeartbeatExport>(null, ex.Message);
            }
        }

        

        /// <summary>
        /// 获取提现记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<WithdrawalsRecordExport> GetWithdrawalsRecordExports(GetWithdrawalsRecordExportsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetWithdrawalsRecordExports(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<WithdrawalsRecordExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取自身帐变记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<GoldsChangeRecordExport> GetGoldsChangeRecords(GetGoldsChangeRecordsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetGoldsChangeRecords(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<GoldsChangeRecordExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取赠送金币记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<GiveGoldsRecordExport> GetGiveGoldsRecords(GetGiveGoldsRecordsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GiveGoldsRecords(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<GiveGoldsRecordExport>(ex.Message);
            }
        }


        /// <summary>
        /// 获取自身的信息
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult<AuthorExport> GetUserSelfInfo(GetUserSelfInfoImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetUserSelfInfo();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult<AuthorExport>(null, ex.Message);
            }
        }


        /// <summary>
        /// 获取充值记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<RechargeRecordExport> GetRechargeRecords(GetRechargeRecordsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetRechargeRecords(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<RechargeRecordExport>(ex.Message);
            }
        }
        #endregion

        #region 操作
        
       
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="import">数据集</param>
        /// <returns>返回操作结果（如成功将返回档次登陆的身份标识）</returns>
        public NormalResult<string> Login(LoginImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    Iworld.Author.Model.Author user = new AuthorManager(db).Login(import);
                    string token = AuthorLoginInfoPond.AddInfo(user.Id);
                    return new NormalResult<string>(token);
                }
            }
            catch (Exception ex)
            {
                return new NormalResult<string>(null, ex.Message);
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="import">数据集</param>
        /// <returns>返回操作结果</returns>
        public NormalResult Register(RegisterImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AuthorManager(db).Create(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

    

      /// <summary>
      /// 充值
      /// </summary>
      /// <param name="import"></param>
      /// <returns></returns>
       public NormalResult Recharge(RechargeImport import)
       {
           try
           {
               using (ModelToDbContext db = new ModelToDbContext())
               {
                   import.CheckAllowExecuteOrNot(db);
                   new RechargeCardManager(db).Recharge(import);
                   return new NormalResult();
               }
           }
           catch (Exception ex)
           {
               return new NormalResult(ex.Message);
           }
       }

        /// <summary>
        /// 赠送金币
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult GiveGolds(GiveGoldsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new GiveGoldsRecordManager(db).Create(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult EditPassword(EditAuthorPasswordImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AuthorManager(db).Update(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }
        
        /// <summary>
        /// 修改二级密码
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult EditTwoLevelPassword(EditTwoLevelPasswordImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AuthorManager(db).Update(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 添加或修改银行信息
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult AddOrEditBank(AddOrEditBankImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AuthorManager(db).Update(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

    
        /// <summary>
        /// 编辑用户自身信息
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult EditBaseInfo(EditAuthorBaseInfoImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AuthorManager(db).Update(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 创建提现记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult CreateWithdrawalsRecord(CreateWithdrawalsRecordImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new WithdrawalsRecordManager(db).Create(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }
        #endregion


    }
}
